@echo off
echo 检查编译环境...
echo.

echo [1/4] 检查Java安装...
where java >nul 2>&1
if %errorlevel% equ 0 (
    echo ✓ Java已安装
    java -version
    echo.
    
    echo 设置JAVA_HOME...
    for /f "tokens=*" %%i in ('where java') do (
        set "JAVA_PATH=%%i"
        for %%j in ("!JAVA_PATH!") (
            set "JAVA_DIR=%%~dpj"
            set "JAVA_HOME=!JAVA_DIR:~0,-1!"
        )
    )
    
    echo JAVA_HOME设置为: %JAVA_HOME%
    echo.
) else (
    echo ✗ Java未安装或不在PATH中
    echo.
    echo 请安装JDK 8或更高版本:
    echo 1. 下载地址: https://adoptium.net/
    echo 2. 安装后确保添加到PATH
    echo 3. 重启命令行窗口
    echo.
    pause
    exit /b 1
)

echo [2/4] 检查Android SDK...
if defined ANDROID_HOME (
    echo ✓ ANDROID_HOME已设置: %ANDROID_HOME%
) else (
    echo ⚠️ ANDROID_HOME未设置
    echo 确保已安装Android SDK
)
echo.

echo [3/4] 检查项目文件...
if exist "app\src\main\java\com\example\appusagestats\model\AppUsageInfo.kt" (
    echo ✓ AppUsageInfo.kt 存在
) else (
    echo ✗ AppUsageInfo.kt 缺失
)

if exist "app\src\main\java\com\example\appusagestats\model\TimeSlot.kt" (
    echo ✓ TimeSlot.kt 存在
) else (
    echo ✗ TimeSlot.kt 缺失
)

if exist "app\src\main\java\com\example\appusagestats\model\UsageEvent.kt" (
    echo ✓ UsageEvent.kt 存在
) else (
    echo ✗ UsageEvent.kt 缺失
)
echo.

echo [4/4] 尝试编译测试...
set "JAVA_HOME=%JAVA_HOME%"
set "PATH=%JAVA_HOME%\bin;%PATH%"

echo 设置后的JAVA_HOME: %JAVA_HOME%
echo.

.\gradlew.bat compileDebugKotlin --no-daemon

if %errorlevel% equ 0 (
    echo ✅ Kotlin编译成功！
    echo 可以继续构建APK
) else (
    echo ❌ Kotlin编译失败
    echo 请检查项目文件或依赖
)

echo.
pause
